import { Command } from 'commander'
import { br } from './branches'
import { logRun } from './lib/base/run'
import { git } from './git/git'
import { autoUpdateCommit } from './auto-update'

export function cmMerge(program: Command, cmd: string, target: (currentBranch: string) => string) {
    program //
        .command(cmd)
        .option('-b, --build', '是否构建')
        .option('-r, --rebase', '是否 rebase')
        .option('-p, --push', '是否推送')
        .option('-t, --transform', '是否转换merge信息分支信息')
        .option('-u, --up', '是否更新')
        .argument('[commit...]', '提交的内容')
        .description(`尝试将分支直接合并到 ${target(br.currentBranch)}`)
        .action((commit: string[], opt: { rebase?: boolean, build?: boolean, push?: boolean, up?: boolean, transform?: boolean }) => {
            const currentBranch = br.currentBranch
            br.mergeToBranch(target(currentBranch), opt.rebase, opt.transform)
            if (opt.build) {
                // 构建 info-im web
                logRun('npm run build:web')
            }
            if (opt.up) {
                autoUpdateCommit(commit.join(' '))
            }
            if (opt.push) {
                git.push()
                git.do('checkout ' + currentBranch)
            }
        })
}